package com.bizhibihui.ordermeal.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bizhibihui.ordermeal.entity.BizCategory;
import com.bizhibihui.ordermeal.entity.BizFood;
import com.bizhibihui.ordermeal.entity.BizTags;
import com.bizhibihui.ordermeal.entity.User;
import com.bizhibihui.ordermeal.service.BizCategoryService;
import com.bizhibihui.ordermeal.service.BizFoodService;
import com.bizhibihui.ordermeal.service.BizFoodTagsService;
import com.bizhibihui.ordermeal.service.BizTagsService;
import com.bizhibihui.ordermeal.util.CoreConst;
import com.bizhibihui.ordermeal.util.Pagination;
import com.bizhibihui.ordermeal.util.ResultUtil;
import com.bizhibihui.ordermeal.vo.FoodConditionVo;
import com.bizhibihui.ordermeal.vo.base.PageResultVo;
import com.bizhibihui.ordermeal.vo.base.ResponseVo;
import lombok.AllArgsConstructor;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 后台美食管理
 *
 */
@Controller
@RequestMapping("food")
@AllArgsConstructor
public class FoodController {

    private final BizFoodService bizFoodService;

    private final BizCategoryService categoryService;

    private final BizTagsService tagsService;

    private final BizFoodTagsService bizFoodTagsService;


    @PostMapping("list")
    @ResponseBody
    public PageResultVo list(FoodConditionVo foodConditionVo, Integer pageNumber, Integer pageSize) {
        IPage<BizFood> page = new Pagination<>(pageNumber, pageSize);
        List<BizFood> articleList = bizFoodService.findByCondition(page, foodConditionVo);
        return ResultUtil.table(articleList, page.getTotal());
    }


    @GetMapping("/add")
    public String addFood(Model model) {
        BizCategory bizCategory = new BizCategory();
        bizCategory.setStatus(CoreConst.STATUS_VALID);
        List<BizCategory> bizCategories = categoryService.selectCategories(bizCategory);
        List<BizTags> tags = tagsService.list();
        model.addAttribute("categories", bizCategories);
        model.addAttribute("tags", tags);
        return CoreConst.ADMIN_PREFIX + "food/add";
    }

    @GetMapping("/edit")
    public String editFood(Model model, Integer id) {
        BizFood bizFood = bizFoodService.selectById(id);
        model.addAttribute("food", bizFood);
        BizCategory bizCategory = new BizCategory();
        bizCategory.setStatus(CoreConst.STATUS_VALID);
        List<BizCategory> bizCategories = categoryService.selectCategories(bizCategory);
        model.addAttribute("categories", bizCategories);
        List<BizTags> sysTags = tagsService.list();
        /*方便前端处理回显*/
        List<BizTags> aTags = new ArrayList<>();
        List<BizTags> sTags = new ArrayList<>();
        boolean flag;
        for (BizTags sysTag : sysTags) {
            flag = false;
            for (BizTags articleTag : bizFood.getTags()) {
                if (articleTag.getId().equals(sysTag.getId())) {
                    BizTags tempTags = new BizTags();
                    tempTags.setId(sysTag.getId());
                    tempTags.setName(sysTag.getName());
                    aTags.add(tempTags);
                    sTags.add(tempTags);
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                sTags.add(sysTag);
            }
        }
        bizFood.setTags(aTags);
        model.addAttribute("tags", sTags);
        return CoreConst.ADMIN_PREFIX + "food/detail";
    }

    @PostMapping("/add")
    @ResponseBody
    public ResponseVo add(BizFood bizFood, Integer[] tag) {
        try {
            bizFood.setStatus(CoreConst.STATUS_VALID);
            BizFood food = bizFoodService.insertFood(bizFood);
            bizFoodTagsService.insertList(tag, food.getId());
            return ResultUtil.success("保存美食成功");
        } catch (Exception e) {
            return ResultUtil.error("保存美食失败");
        }
    }


    @PostMapping("/edit")
    @ResponseBody
    public ResponseVo edit(BizFood bizFood, Integer[] tag) {
        bizFoodService.updateById(bizFood);
        bizFoodTagsService.removeByFoodId(bizFood.getId());
        bizFoodTagsService.insertList(tag, bizFood.getId());
        return ResultUtil.success("编辑美食成功");
    }

    @PostMapping("/delete")
    @ResponseBody
    public ResponseVo delete(Integer id) {
        int i = bizFoodService.deleteBatch(new Integer[]{id});
        if (i > 0) {
            return ResultUtil.success("删除美食成功");
        } else {
            return ResultUtil.error("删除美食失败");
        }
    }

    @PostMapping("/batch/delete")
    @ResponseBody
    public ResponseVo deleteBatch(@RequestParam("ids[]") Integer[] ids) {
        int i = bizFoodService.deleteBatch(ids);
        if (i > 0) {
            return ResultUtil.success("删除美食成功");
        } else {
            return ResultUtil.error("删除美食失败");
        }
    }
}
